# coefplots.R
# Outputs: Figures in paper and appendix
# Dependencies: depends on files created by models_paper_january2020.do

library(tidyverse)
library(foreign)
library(ggpubr)
library(extrafont)
library(fontcm)
loadfonts()
library(dotwhisker)
library(gridExtra)

setwd('')

# year: '' for full sample or _1990
year <- ''  # '_1990'

# ------------------------------------------------------------------------------
# Fig 1
# ------------------------------------------------------------------------------

df <- read.dta(paste0('models/figure1', year, '.dta')) %>% as_tibble()

names(df) <- c('term', 'estimate', 'std.error', 'ci_lower', 'ci_upper',
               'n', 'r2', 'model', 'specification', 'operationalization')

df$statistic <- df$estimate / df$std.error

# Fig 1 (Manuscript) -----------------------------------------------------------

dfig1 <- df %>% filter(specification == 'FE5') %>%
  mutate(term = str_remove(term, 'd_'),
         term = ordered(term, levels = rev(df$term[6:10])),
         model = operationalization,
         continuous = grepl('share', term))

fig1 <-
  dwplot(dfig1, model_name = 'operationalization',
         dot_args = list(aes(color = NULL, shape = model), fill = 'white',
                         size = 3),
         whisker_args = list(aes(color = NULL), color = 'black'),
         vline = geom_vline(xintercept = 0, linetype = 'dashed', color = 'gray'),
         dodge_size = .6) +
  scale_y_discrete(labels = c('populistparty' = 'Populist Participation',
                              'l_populistpresence' = 'Populist Representation',
                              'populist_new' = "New Populist Party",
                              'l_populistvoteshare' = "Populist Vote Share",
                              'l_populistseatshare' = "Populist Seat Share")) +
  scale_shape_manual(name = '', values = c(22, 15),
                     labels = c('level' = 'Levels', 'delta' = expression(Delta))) +
  # facet_wrap(~continuous) +
  guides(shape = guide_legend(reverse=T)) +
  theme_bw(base_size = 20) +
  theme(panel.grid = element_blank(), legend.position = 'bottom')

# PDF
ggsave(filename = paste0('figures/f1', year, '.pdf'), plot = fig1, width = 10, height = 6,
       useDingbats = FALSE, device = cairo_pdf())
# embed_fonts(paste0('figures/f1', year, '.pdf'))

# pdf(file = 'figures/f1.pdf', width = 10, height = 6, family = 'Times New Roman')
# fig1
# dev.off()

#PNG
ggsave(filename = paste0('figures/f1', year, '.pdf'), plot = fig1, width = 10, height = 6)

# Fig 1 (Appendix) -----------------------------------------------------------

dfig1a <- df %>%
  mutate(term = ordered(term, levels = rev(df$term[c(1, 6, 2, 7, 3, 8, 4, 9, 5, 10)])),
         model = specification,
         continuous = grepl('share', term))

# Dummy
fig1_dummy <-
  dwplot(filter(dfig1a, continuous == F), model_name = 'specification',
         dot_args = list(aes(color = NULL, shape = specification), fill = 'white',
                         size = 3),
         whisker_args = list(aes(color = NULL), color = 'black'),
         vline = geom_vline(xintercept = 0, linetype = 'dashed', color = 'gray'),
         dodge_size = .6) +
  scale_y_discrete(labels = c('populistparty' = 'Populist Participation',
                              'l_populistpresence' = 'Populist Representation',
                              'populist_new' = "New Populist Party",
                              'l_populistvoteshare' = "Populist Vote Share",
                              'l_populistseatshare' = "Populist Seat Share",
                              'd_populistparty' = expression(Delta*'Populist Participation'),
                              'd_l_populistpresence' = expression(Delta*'Populist Representation'),
                              'd_populist_new' = expression(Delta*'New Populist Party'),
                              'd_l_populistvoteshare' = expression(Delta*"Populist Vote Share"),
                              'd_l_populistseatshare' = expression(Delta*'Populist Seat Share'))) +
  scale_shape_manual(name = '', values = c(22, 15, 21, 19),
                     labels = c('FE' = 'Year FE', 'FE5' = 'Period FE',
                                'PWFE5' = 'PW Period FE', 'PWFE' = 'PW Year FE')) +
  guides(shape = guide_legend(reverse=T)) +
  theme_bw(base_size = 20, base_family = 'CMU Serif') +
  theme(panel.grid = element_blank(), legend.position = 'none')

# Continuous
fig1_continuous <-
  dwplot(filter(dfig1a, continuous == T), model_name = 'specification',
         dot_args = list(aes(color = NULL, shape = specification), fill = 'white',
                         size = 3),
         whisker_args = list(aes(color = NULL), color = 'black'),
         vline = geom_vline(xintercept = 0, linetype = 'dashed', color = 'gray'),
         dodge_size = .6) +
  scale_y_discrete(labels = c('populistparty' = 'Populist Participation',
                              'l_populistpresence' = 'Populist Representation',
                              'populist_new' = "New Populist Party",
                              'l_populistvoteshare' = "Populist Vote Share",
                              'l_populistseatshare' = "Populist Seat Share",
                              'd_populistparty' = expression(Delta*'Populist Participation'),
                              'd_l_populistpresence' = expression(Delta*'Populist Representation'),
                              'd_populist_new' = expression(Delta*'New Populist Party'),
                              'd_l_populistvoteshare' = expression(Delta*'Populist Vote Share'),
                              'd_l_populistseatshare' = expression(Delta*'Populist Seat Share'))) +
  scale_shape_manual(name = '', values = c(22, 15, 21, 19),
                     labels = c('FE' = 'Year FE', 'FE5' = 'Period FE',
                                'PWFE5' = 'PW Period FE', 'PWFE' = 'PW Year FE')) +
  guides(shape = guide_legend(reverse=T)) +
  theme_bw(base_size = 20, base_family = 'CMU Serif') +
  theme(panel.grid = element_blank(), legend.position = 'bottom')

# Figure
fig1a <-
grid.arrange(fig1_dummy, fig1_continuous, heights = c(6, 5))

# PDF
ggsave(filename = paste0('figures/f1_appendix', year, '.pdf'), plot = fig1a,
       width = 10, height = 10,
       useDingbats = FALSE, device = cairo_pdf())
# embed_fonts(paste0('figures/f1_appendix', year, '.pdf'))

# ------------------------------------------------------------------------------
# Fig 2
# ------------------------------------------------------------------------------

df2 <- read.dta(paste0('models/figure2', year,'.dta')) %>% as_tibble()

names(df2) <- c('term', 'estimate', 'std.error', 'ci_lower', 'ci_upper',
                'n', 'r2', 'model', 'specification', 'subsample')

df2$statistic <- df2$estimate / df2$std.error

# Fig 2 (Manuscript) -----------------------------------------------------------

dfig2 <- df2 %>% mutate(model = specification) %>%
  mutate(term = paste(term, subsample, sep = '_'),
         term = ordered(term, levels = c('l_populistpresence_east',
                                         'd_l_populistpresence_east',
                                         'l_populistpresence_west',
                                         'd_l_populistpresence_west')))

fig2 <-
  dwplot(dfig2, model_name = 'specification',
         dot_args = list(aes(color = NULL, shape = model), fill = 'white',
                         size = 3),
         whisker_args = list(aes(color = NULL), color = 'black'),
         vline = geom_vline(xintercept = 0, linetype = 'dashed', color = 'gray'),
         dodge_size = .6) +
  geom_hline(yintercept = 2.5, linetype = 'dashed') +
  annotate('text', x = -2, y = 2.5, label = 'CEE', vjust = -1, family = 'Times New Roman') +
  annotate('text', x = -2, y = 2.4, label = 'WE', vjust = 1, family = 'Times New Roman') +
  scale_x_continuous(limits = c(-3, 14), breaks = seq(-2, 14, 2)) +
  scale_y_discrete(labels = c('l_populistpresence_east' = 'Populist Representation',
                              'd_l_populistpresence_east' =
                                expression(Delta*'Populist Representation'),
                              'l_populistpresence_west' = 'Populist Representation',
                              'd_l_populistpresence_west' =
                                expression(Delta*'Populist Representation'))) +
  scale_shape_manual(name = '', values = c(22, 15, 21, 19),
                     labels = c('FE' = 'Year FE', 'FE5' = 'Period FE',
                                'PWFE5' = 'PW Period FE', 'PWFE' = 'PW Year FE')) +
  guides(shape = guide_legend(reverse=T)) +
  theme_bw(base_family = 'Times New Roman', base_size = 20) +
  theme(panel.grid = element_blank(), legend.position = 'bottom')

# PDF
ggsave(filename = paste0('figures/f2', year, '.pdf'), plot = fig2,
       width = 10, height = 6,
       useDingbats = FALSE)
# embed_fonts(paste0('figures/f2', year, '.pdf'))

#PNG
ggsave(filename = paste0('figures/f2', year, '.png'), plot = fig2, width = 10, height = 6)

# ------------------------------------------------------------------------------
# Fig 3
# ------------------------------------------------------------------------------

df3 <- read.dta(paste0('models/figure3', year, '.dta')) %>% as_tibble()

names(df3) <- c('term', 'estimate', 'std.error', 'ci_lower', 'ci_upper',
                'n', 'r2', 'model', 'specification', 'subsample')

df$statistic <- df$estimate / df$std.error

# Fig 3 (Manuscript) -----------------------------------------------------------

sample <- 'east'

dfig3 <- df3 %>% filter(subsample == sample) %>%
  mutate(model = specification,
         term = ordered(term, levels = c('l_populistpresence_left',
                                         'l_populistpresence_right',
                                         'd_l_populistpresence_left',
                                         'd_l_populistpresence_right')))

fig3 <-
  dwplot(dfig3, model_name = 'specification',
         dot_args = list(aes(color = NULL, shape = model), fill = 'white',
                         size = 3),
         whisker_args = list(aes(color = NULL), color = 'black'),
         vline = geom_vline(xintercept = 0, linetype = 'dashed', color = 'gray'),
         dodge_size = .6) +
  scale_y_discrete(labels = c('l_populistpresence_left' = 'Populist Representation (Left)',
                              'l_populistpresence_right' = 'Populist Representation (Right)',
                              'd_l_populistpresence_left' =
                                expression(Delta*'Populist Representation (Left)'),
                              'd_l_populistpresence_right' =
                                expression(Delta*'Populist Representation (Right)'))) +
  scale_shape_manual(name = '', values = c(22, 15, 21, 19),
                     labels = c('FE2' = 'Year FE', 'FE5' = 'Period FE',
                                'PWFE5' = 'PW Period FE', 'PWFE' = 'PW Year FE')) +
  guides(shape = guide_legend(reverse=T)) +
  theme_bw(base_family = 'Times New Roman', base_size = 19) +
  theme(panel.grid = element_blank(), legend.position = 'bottom')

# PDF
ggsave(filename = paste0('figures/f3_', sample, year, '.pdf'), plot = fig3,
       width = 10, height = 6,
       useDingbats = FALSE)
# embed_fonts(paste0('figures/f3', year, '.pdf'))

#PNG
ggsave(filename = paste0('figures/f3_', sample, year, '.png'), plot = fig3,
       width = 10, height = 6)